package com.quanyan.user.web;

import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.assets.entity.TbUserAssets;
import com.quanyan.assets.request.ReqCoupon;
import com.quanyan.assets.request.ReqUserInfo;
import com.quanyan.assets.response.RespOrderCoupon;
import com.quanyan.assets.service.CouponService;
import com.quanyan.assets.service.UserAssetsService;
import com.quanyan.place.client.PlaceClient;
import com.quanyan.user.model.vo.response.UserAssetsBaseInfo;
import com.quanyan.user.service.HomePageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by hua on 2016/10/12.
 * <p>
 * 用户资产Controller
 */
@RestController
@RequestMapping("/api/user/assets")
public class UserAssetsController {
    private static final Logger logger = LoggerFactory.getLogger(UserAssetsController.class);

    @Autowired
    private CouponService couponService;

    @Autowired
    private UserAssetsService userAssetsService;

    @Autowired
    private HomePageService homePageService;

    @Autowired
    private PlaceClient placeClient;

    /**
     * 获取用户优惠券列表
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/userCouponList", method = RequestMethod.POST, produces = "application/json")
    public APIResponse<RespOrderCoupon> getReceiveCouponList(@RequestBody Map map, HttpServletRequest request) {
        Integer uid = (Integer) map.get("uid");
        Integer status = (Integer) map.get("status");
        Integer pageNo = (Integer) map.get("pageNo");
        Integer pageSize = (Integer) map.get("pageSize");
        if (null == uid) {
            return APIResponse.returnFail("请求参数不正确");
        }
        if (null == status) {
            return APIResponse.returnFail("请求参数不正确");
        }

        APIRequest<ReqCoupon> apiRequest = new APIRequest<>();
        ReqCoupon reqCoupon = new ReqCoupon();
        reqCoupon.setUid(uid);
        reqCoupon.setStatus((byte) status.intValue());
        if (pageNo != null)
            reqCoupon.setPageNo(pageNo);
        if (pageSize != null)
            reqCoupon.setPageSize(pageSize);
        apiRequest.setVer((String)map.get("ver"));
        apiRequest.setData(reqCoupon);
        APIResponse<RespOrderCoupon> userCouponList = null;
        try {
            userCouponList = couponService.getReceiveCouponList(apiRequest);
        } catch (Exception e) {
            logger.error("获取用户用户券列表失败:{}", e.getMessage());
            return APIResponse.returnFail("获取用户用户券列表失败");
        }

        return userCouponList;
    }

    /**
     * 获取用户资产列表
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/userAccumulateList", method = RequestMethod.POST, produces = "application/json")
    public APIResponse<List<TbUserAssets>> getUserAccumulateList(@RequestBody Map map, HttpServletRequest request) {
        Integer uid = (Integer) map.get("uid");
        Integer assetsType = (Integer) map.get("assetsType");

        if (null == uid) {
            return APIResponse.returnFail("请求参数不正确");
        }
        if (null == assetsType) {
            return APIResponse.returnFail("请求参数不正确");
        }

        APIRequest<ReqUserInfo> apiRequest = new APIRequest<>();
        ReqUserInfo userInfo = new ReqUserInfo();
        userInfo.setUid(uid);
        userInfo.setAssetsType((byte) assetsType.intValue());
        apiRequest.setData(userInfo);
        APIResponse<List<TbUserAssets>> userAssets = null;

        try {
            userAssets = userAssetsService.getUserAccumulateList(apiRequest);
        } catch (Exception e) {
            logger.error("获取用户资产列表失败");
            return APIResponse.returnFail("获取用户资产列表失败");
        }

        return userAssets;
    }

    /**
     * 获取用户资产基本信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserAssetsBaseInfo", method = RequestMethod.POST, produces = "application/json")
    public APIResponse getUserAssetsBaseInfo(HttpServletRequest request){
        Integer uid = (Integer) request.getAttribute("uid");
        if (null == uid) {
            return APIResponse.returnFail(ApiResponseEnum.COOKIES_IS_NULL);
        }

        try {
            APIRequest<ReqCoupon> apiRequest1 = new APIRequest<>();
            ReqCoupon reqCoupon = new ReqCoupon();
            reqCoupon.setUid(uid);
            reqCoupon.setStatus((byte)1);
            reqCoupon.setPageNo(0);
            reqCoupon.setPageSize(5);
            apiRequest1.setData(reqCoupon);
            APIResponse<RespOrderCoupon> userCouponList = null;
            userCouponList = couponService.getReceiveCouponList(apiRequest1);

            APIRequest<ReqUserInfo> apiRequest2 = new APIRequest<>();
            ReqUserInfo userInfo = new ReqUserInfo();
            userInfo.setUid(uid);
            userInfo.setAssetsType((byte)1);
            apiRequest2.setData(userInfo);
            APIResponse<List<TbUserAssets>> userAssets = userAssetsService.getUserAccumulateList(apiRequest2);

            //设置优惠券数量
            UserAssetsBaseInfo userAssetsBaseInfo = new UserAssetsBaseInfo();
            if (userCouponList != null && userCouponList.getData() != null ) {
                userAssetsBaseInfo.setCouponNum(userCouponList.getData().getUseAmount());
            }

            //设置鸟蛋数量
            if (userAssets != null && userAssets.getData() != null && userAssets.getData().size() > 0 ) {
                TbUserAssets tbUserAssets = userAssets.getData().get(0);
                userAssetsBaseInfo.setEggsNum(tbUserAssets.getAccumulate().intValue());
            }

            // 设置会员卡数量
            Integer memberCardNum = placeClient.getMemberCardNum(uid);
            userAssetsBaseInfo.setMemberCardNum( (memberCardNum == null)? 0 : memberCardNum);

            return APIResponse.returnSuccess(userAssetsBaseInfo);

        }catch (Exception e) {
            logger.error("获取用户资产信息失败:{}", e);
            return APIResponse.returnFail("获取用户资产信息失败");
        }

    }

}
